package com.sjm.suchbear.minibookmall.action;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sjm.suchbear.minibookmall.model.UserModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Suchbear on 2018/5/5.
 */
public class UserManage {

    //新增用户
    public static void addUser(SQLiteDatabase db, String username, String password, String realname, String phone, String address, int auth, String cost) {
        ContentValues values = new ContentValues();
        values.put("username", username);
        values.put("password", password);
        values.put("realname", realname);
        values.put("phone", phone);
        values.put("address", address);
        values.put("auth", auth);
        values.put("cost", cost);
        db.insert("mall_user", null, values);
    }

    //修改用户个人信息
    public static void changeUserInfo(SQLiteDatabase db, String userID, String realname, String phone, String address) {
        ContentValues values = new ContentValues();
        values.put("realname", realname);
        values.put("phone", phone);
        values.put("address", address);
        db.update("mall_user", values, "userID=?", new String[]{userID});
    }

    //修改用户钱包余额
    public static void changeUserCost(SQLiteDatabase db, String userID, String cost) {
        ContentValues values = new ContentValues();
        values.put("cost", cost);
        db.update("mall_user", values, "userID=?", new String[]{userID});
    }

    //根据用户ID查询用户
    public static UserModel findUserByID(SQLiteDatabase db, String userID) {
        Cursor cursor = db.query("mall_user", new String[]{"userID", "username", "realname", "phone", "address", "auth", "cost"},
                "userID=?", new String[]{userID}, null, null, null);
        if (cursor.moveToNext()) {
            UserModel userModel = new UserModel();
            userModel.setUserID(userID);
            userModel.setUsername(cursor.getString(1));
            userModel.setRealname(cursor.getString(2));
            userModel.setPhone(cursor.getString(3));
            userModel.setAddress(cursor.getString(4));
            userModel.setAuth(cursor.getInt(5));
            userModel.setCost(cursor.getString(6));
            return userModel;
        }
        cursor.close();
        return null;
    }

    //根据用户名密码登录查询
    public static UserModel findUserByName(SQLiteDatabase db, String username, String password) {
        Cursor cursor = db.query("mall_user", new String[]{"userID", "username", "realname", "phone", "address", "auth", "cost"},
                "username=? and password=?", new String[]{username, password}, null, null, null);
        if (cursor.moveToNext()) {
            UserModel userModel = new UserModel();
            userModel.setUserID(cursor.getString(0));
            userModel.setUsername(cursor.getString(1));
            userModel.setRealname(cursor.getString(2));
            userModel.setPhone(cursor.getString(3));
            userModel.setAddress(cursor.getString(4));
            userModel.setAuth(cursor.getInt(5));
            userModel.setCost(cursor.getString(6));
            return userModel;
        }
        cursor.close();
        return null;
    }

    public static List<UserModel> getUserList(SQLiteDatabase db) {
        List<UserModel> users = new ArrayList<>();
        Cursor cursor = db.query("mall_user", new String[]{"userID", "username", "realname", "phone", "address", "auth", "cost"},
                null, null, null, null, null);
        if (cursor.getCount() > 0) {
            for (int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToNext();
                UserModel userModel = new UserModel();
                userModel.setUserID(cursor.getString(0));
                userModel.setUsername(cursor.getString(1));
                userModel.setRealname(cursor.getString(2));
                userModel.setPhone(cursor.getString(3));
                userModel.setAddress(cursor.getString(4));
                userModel.setAuth(cursor.getInt(5));
                userModel.setCost(cursor.getString(6));
                users.add(userModel);
            }
            return users;
        }
        cursor.close();
        return null;
    }

    public static boolean vaildUsername(SQLiteDatabase db, String username) {
        boolean isVaild = true;
        Cursor cursor = db.query("mall_user", new String[]{"userID"},
                "username=?", new String[]{username}, null, null, null);
        if (cursor.moveToNext()) {
            isVaild = false;
        }
        return isVaild;
    }
}
